/**
 * 
 */
package dt.inlee.system.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import dt.inlee.common.dao.impl.BaseDao;
import dt.inlee.common.exception.FrameworkException;
import dt.inlee.common.util.BaseUtil;
import dt.inlee.domain.DesktopDefend;

/**
 * @功能说明：桌面维护数据访问层
 * @作者： xgyin
 * @创建日期： 2010-12-22
 */
@Repository("desktopDefendDao")
public class DesktopDefendDao extends BaseDao {
	/**
	 * 根据条件获取桌面管理总记录数
	 * @param params
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public int getDesktopDefendTotalRows(String title, String divId) throws FrameworkException {

		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(
				DesktopDefend.class);
		if (!BaseUtil.isEmpty(divId))
			criteria.add(Restrictions.eq("divId", divId));
		if (!BaseUtil.isEmpty(title))
			criteria.add(Restrictions.like("title", "%" + title + "%"));
		criteria.setProjection(Projections.countDistinct("id"));
		List<Integer> count = criteria.list();
		return count.get(0).intValue();
	}

	/**
	 * 根据条件获取桌面管理记录
	 * @param params
	 * @param curPage
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<DesktopDefend> findAllDesktopDefends(String title, String divId, int curPage, int pageSize)
			throws FrameworkException {

		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(
				DesktopDefend.class);
		if (!BaseUtil.isEmpty(divId))
			criteria.add(Restrictions.eq("divId", divId));
		if (!BaseUtil.isEmpty(title))
			criteria.add(Restrictions.like("title", "%" + title + "%"));
		if (curPage > 0 && pageSize > 0) {
			criteria.setFirstResult((curPage - 1) * pageSize);
			criteria.setMaxResults(pageSize);
		}
		return criteria.list();

	}

	/**
	 * 通过桌面管理ID、区域业务名称和DIV标签的ID查找桌面管理
	 * @param params
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public DesktopDefend findDesktopDefendByCon(String id, String title, String divId) throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(
				DesktopDefend.class);
		criteria.add(Restrictions.ne("id", id));
		criteria.add(Restrictions.or(Restrictions.eq("title", title), Restrictions.eq("divId", divId)));
		List<DesktopDefend> defends = criteria.list();
		if (!BaseUtil.isEmpty(defends) && defends.size() > 0)
			return defends.get(0);
		return null;

	}

	/**
	 * 获取自己的桌面业务模块
	 * @param loginUserId
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public List<DesktopDefend> findSelfDesktopDefends(String loginUserId) throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(
				DesktopDefend.class);
		Criteria selfDesk = criteria.createCriteria("selfDesktopDefends");
		selfDesk.add(Restrictions.eq("defendUser.id", loginUserId));
		return criteria.list();
	}

	/**
	 * 通过桌面管理英文名称和中文名称查找桌面管理
	 * @param title
	 * @param divId
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public DesktopDefend findDesktopDefendyByName(String title, String divId) throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(
				DesktopDefend.class);
		criteria.add(Restrictions.or(Restrictions.eq("title", title), Restrictions.eq("divId", divId)));
		List<DesktopDefend> defends = criteria.list();
		if (!BaseUtil.isEmpty(defends) && defends.size() > 0)
			return defends.get(0);
		return null;

	}

}
